package dongshi.daddy.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;

@Activate(group = Constants.CONSUMER, order = 10002)
public class DubboHystrixFilter implements Filter {

     // 通过该方法完成调用服务提供者的服务
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String isOpen = invoker.getUrl().getMethodParameter(invocation.getMethodName(), "isOpen");
        // 是否打开hystrix
        if (isOpen != null && Boolean.parseBoolean(isOpen)) {
            DubboHystrixCommand dubboHystrixCommand = new DubboHystrixCommand(invoker, invocation);
            Result result = dubboHystrixCommand.execute();
            return result;
        } else {
            // 未打开的话, 直接走真正调用逻辑
            return invoker.invoke(invocation);
        }
    }
}